use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("Guess the number!");
    let secret_number = rand::thread_rng().gen_range(1..101);
    println!("The secret number is: {}", secret_number);

    let mut guess = String::new();
    let mut i = 0;
    while i < 10 
    {
        println!("Please input your guess:");
        io::stdin().read_line(&mut guess).expect("Failed to read line");

        println!("You guessed: {}", guess);

        let guess_number: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please input a valid number.");
                guess.clear();
                continue;
            }
        }; 

        // if guess_number == secret_number 
        // {
        //     println!("Congratulations, you guessed the number!");
        //     break;
        // } else if guess_number < secret_number 
        // {
        //     println!("Too small, try again!");
        // } 
        // else 
        // {
        //     println!("Too big, try again!");
        // }

        match guess_number.cmp(&secret_number) {
            Ordering::Less => println!("Too small, try again!"),
            Ordering::Greater => println!("Too big, try again!"),
            Ordering::Equal => {println!("Congratulations, you guessed the number!");break},
        }

        i += 1;
        guess.clear();
    } 
    if i == 10 
    {
        println!("Sorry, you have run out of guesses. The secret number was: {}", secret_number);
    }

    println!("Game over.");
}
